<?php
/*
库存列表
*/
class Con_stock extends Acan{
	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Acan_table_stock);
		if($_POST){Afun_cache_rm('Aproduct');}//有数据库操作删除导航缓存
	}
	function Act_list(){
		if(empty($this->title)){$this->title = '库存列表';}
		//分页函数
		$con->select = "a.*,p.`name`";
		if($this->id > 0){$con->where = "a.`pid`='$this->id'";}
		$con->join = "LEFT JOIN `".Acan_table_product."` p ON a.`pid`=p.`pid`";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '50';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		$tcon->select = "SUM(a.cost*a.num) as total,SUM(a.num) as nums";
		$total = $this->model->find($tcon);
		include $this->Atpl();
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		foreach($_POST['save'] as $i=>$vals){
			$this->model->updatePk($vals,$vals['id']);
		}
		echo '保存成功';
	//	Afun_refresh('保存成功','referrer');
	}
	//添加
	function Act_add(){
		Afun_post_auth();
		$_POST['add']['pid'] = $this->id;
		if($this->model->save($_POST['add'])){
			Afun_refresh('添加成功','referrer');
		}else{Afun_refresh('添加失败','referrer');}
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	
}

?>